<?php

// 本类由系统自动生成，仅供测试用途
class ManageCenterAction extends BaseAction {
	public function index() {
		header("Content-Type:text/html; charset=utf-8");
		$this->display();
	}

	/**
	 * 进入修改站点信息的界面
	 */
	public function getSiteInfo() {
		$model = D("ny_stieinfo");
		$siteInfo = $model->find();
		$this->assign("siteInfo", $siteInfo);
		$this->display();
	}

	/**
	 * 保存站点信息
	 */
	public function saveStieInfo() {
		$model = D("ny_stieinfo");
		$data['domain'] = $_REQUEST['domain'];
		$data['title'] = $_REQUEST['title'];
		$data['keyword'] = $_REQUEST['keyword'];
		$data['description'] = $_REQUEST['description'];
		$data['aboutuserpath'] = $_REQUEST['aboutuserpath'];
		$model->where("id=" . $_REQUEST['id'])->save($data);
		$this->redirect("getSiteInfo", null, 1, '保存成功');
	}

	public function fileUpload() {
		$targetFolder = '/Public/uploads'; // Relative to the root

		if (!empty ($_FILES)) {
			$tempFile = $_FILES['Filedata']['tmp_name'];

			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder . "/siteinfo/";
			
			if (!file_exists(rtrim($targetPath, '/'))) {
				//路径不存在就创建
				$this->mkdirs($targetPath);
			}
			$kzm = substr(strrchr($_FILES['Filedata']['name'], '.'), 1);
			$targetFile = rtrim($targetPath, '/') . '/' . time().".".$kzm;
			$path = $targetFolder . "/siteinfo/".time().".".$kzm;
			// Validate the file type
			$fileTypes = array (
				'jpg',
				'jpeg',
				'gif',
				'png'
			); // File extensions

			$fileParts = pathinfo($_FILES['Filedata']['name']);

			if (in_array($fileParts['extension'], $fileTypes)) {
				if(move_uploaded_file($tempFile, iconv("utf-8", "gb2312", $targetFile))){
					//把文件地址保存到数据
					$model = D("ny_stieinfo");
					$data['aboutuserpath'] = $path;
					$model->where("id=1")->save($data);
				}
				echo $path;
			} else {
				echo 'Invalid file type.';
			}
		}
	}
	
	public function yhUpload() {
		$targetFolder = '/Public/uploads/youhui/'; // Relative to the root

		if (!empty ($_FILES)) {
			$tempFile = $_FILES['Filedata']['tmp_name'];

			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
			
			if (!file_exists(rtrim($targetPath, '/'))) {
				//路径不存在就创建
				$this->mkdirs($targetPath);
			}
			$kzm = substr(strrchr($_FILES['Filedata']['name'], '.'), 1);
			$targetFile = rtrim($targetPath, '/') . '/' . time().".".$kzm;
			$path = $targetFolder .time().".".$kzm;
			// Validate the file type
			$fileTypes = array (
				'jpg',
				'jpeg',
				'gif',
				'png'
			); // File extensions

			$fileParts = pathinfo($_FILES['Filedata']['name']);

			if (in_array($fileParts['extension'], $fileTypes)) {
				if(move_uploaded_file($tempFile, iconv("utf-8", "gb2312", $targetFile))){
					//把文件地址保存到数据
					$model = D("ny_indexset");
					$data['imgUrl'] = $path;
					$model->where("type='YH".$_REQUEST['sort']."'")->save($data);
				}
				echo $path;
			} else {
				echo 'Invalid file type.';
			}
		}
	}
	
	public function wkUpload() {
		$targetFolder = '/Public/uploads/wk/'; // Relative to the root

		if (!empty ($_FILES)) {
			$tempFile = $_FILES['Filedata']['tmp_name'];

			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
			
			if (!file_exists(rtrim($targetPath, '/'))) {
				//路径不存在就创建
				$this->mkdirs($targetPath);
			}
			$kzm = substr(strrchr($_FILES['Filedata']['name'], '.'), 1);
			$targetFile = rtrim($targetPath, '/') . '/' . time().".".$kzm;
			$path = $targetFolder .time().".".$kzm;
			// Validate the file type
			$fileTypes = array (
				'jpg',
				'jpeg',
				'gif',
				'png'
			); // File extensions

			$fileParts = pathinfo($_FILES['Filedata']['name']);

			if (in_array($fileParts['extension'], $fileTypes)) {
				if(move_uploaded_file($tempFile, iconv("utf-8", "gb2312", $targetFile))){
					//把文件地址保存到数据
					$model = D("ny_indexset");
					$data['imgUrl'] = $path;
					$model->where("type='WK1'")->save($data);
				}
				echo $path;
			} else {
				echo 'Invalid file type.';
			}
		}
	}
	
	public function bannerUpload() {
		$targetFolder = '/Public/uploads/banner/'; // Relative to the root

		if (!empty ($_FILES)) {
			$tempFile = $_FILES['Filedata']['tmp_name'];

			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
			
			if (!file_exists(rtrim($targetPath, '/'))) {
				//路径不存在就创建
				$this->mkdirs($targetPath);
			}
			$kzm = substr(strrchr($_FILES['Filedata']['name'], '.'), 1);
			$targetFile = rtrim($targetPath, '/') . '/' . time().".".$kzm;
			$path = $targetFolder .time().".".$kzm;
			// Validate the file type
			$fileTypes = array (
				'jpg',
				'jpeg',
				'gif',
				'png'
			); // File extensions

			$fileParts = pathinfo($_FILES['Filedata']['name']);

			if (in_array($fileParts['extension'], $fileTypes)) {
				if(move_uploaded_file($tempFile, iconv("utf-8", "gb2312", $targetFile))){
					//把文件地址保存到数据
					$model = D("ny_indexset");
					$data['imgUrl'] = $path;
					$model->where("type='BANNER".$_REQUEST['sort']."'")->save($data);
				}
				echo $path;
			} else {
				echo 'Invalid file type.';
			}
		}
	}
	
	/*
	 * 保存banner的连接
	 */
	public function saveBannerUrl(){
		$data1['clickUrl'] = $_REQUEST['url1'];
		$data2['clickUrl'] = $_REQUEST['url2'];
		$data3['clickUrl'] = $_REQUEST['url3'];
		$data1['title'] = $_REQUEST['text1'];
		$data2['title'] = $_REQUEST['text2'];
		$data3['title'] = $_REQUEST['text3'];
		
		$model = D("ny_indexset");
		$model->where("type='BANNER1'")->save($data1);
		$model->where("type='BANNER2'")->save($data2);
		$model->where("type='BANNER3'")->save($data3);
		
		$this->redirect("indexSet", null, 1, '保存成功');
	}
	
	public function saveYhUrl(){
		$data1['clickUrl'] = $_REQUEST['url1'];
		$data2['clickUrl'] = $_REQUEST['url2'];
		$data1['title'] = $_REQUEST['text1'];
		$data2['title'] = $_REQUEST['text2'];
		
		$model = D("ny_indexset");
		$model->where("type='YH1'")->save($data1);
		$model->where("type='YH2'")->save($data2);
		
		$this->redirect("indexSet", null, 1, '保存成功');
	}
	
	public function saveWkUrl(){
		$data1['clickUrl'] = $_REQUEST['url'];
		$data1['title'] = $_REQUEST['text'];
		
		$model = D("ny_indexset");
		$model->where("type='WK1'")->save($data1);
		
		$this->redirect("indexSet", null, 1, '保存成功');
	}

	public function mkdirs($dir) {
		if (!is_dir($dir)) {
			if (!$this->mkdirs(dirname($dir))) {
				return false;
			}
			if (!mkdir($dir, 0777)) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 定制首页的一些信息
	 */
	public function indexSet(){
		//查询banner
		$model = D("ny_indexset");
		$banner1 = $model->where("type='BANNER1'")->find();
		$banner2 = $model->where("type='BANNER2'")->find();
		$banner3 = $model->where("type='BANNER3'")->find();
		$this->assign("banner1",$banner1);
		$this->assign("banner2",$banner2);
		$this->assign("banner3",$banner3);
		
		//查询
		$yh1 = $model->where("type='YH1'")->find();
		$yh2 = $model->where("type='YH2'")->find();
		$this->assign("yh1",$yh1);
		$this->assign("yh2",$yh2);
		
		//查询workshop
		$wk = $model->where("type='WK1'")->find();
		$this->assign("wk",$wk);
		
		$this->display();
	}
}